package com.baizhi.controller;

import com.baizhi.entity.Admin;
import com.baizhi.entity.Album;
import com.baizhi.entity.Banner;
import com.baizhi.service.AdminService;
import com.baizhi.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2019/4/3.
 */
@Controller
@RequestMapping("/Album")
public class AlbumController{
    @Autowired
    private AlbumService albumService;



    @RequestMapping("/selectAllAlbum.do")
    @ResponseBody
    public Map selectAllAlbum(int rows,int page,String albumName){
        return albumService.selectAllAlbum(rows,page,albumName);
    };


    @ResponseBody
    @RequestMapping("/addAlbum.do")
    public Map addAlbum(HttpServletRequest request, Album album, MultipartFile Image){
        System.out.println(album+"---------------");
        if(Image!=null){
            String fileName=Image.getOriginalFilename();
            String st = UUID.randomUUID().toString();
            String newName=st+fileName;
            String realPathNewMethod = request.getServletContext().getRealPath("img/audioCollection");
            try {
                Image.transferTo(new File(realPathNewMethod+"/"+newName));
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            album.setAlbumImage("/audioCollection/"+newName);
            return  albumService.addAlbum(album);
        }else {
            album.setAlbumImage("/img/audioCollection"+"1.jpg");
            return  albumService.addAlbum(album);
        }
    }






    @ResponseBody
    @RequestMapping("/delateAlbum.do")
    public Map delateAlbum(Integer[] ids){
        return  albumService.delatebanner(ids);
    }



    @ResponseBody
    @RequestMapping("/updateAlbum.do")
    public Map updateAlbum(HttpServletRequest request, Album album, MultipartFile updateAlbumImage){
        if(updateAlbumImage!=null){
            String fileName=updateAlbumImage.getOriginalFilename();
            String st = UUID.randomUUID().toString();
            String newName=st+fileName;
            String realPathNewMethod = request.getServletContext().getRealPath("audioImage");
            try {
                updateAlbumImage.transferTo(new File(realPathNewMethod+"/"+newName));
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            album.setAlbumImage("/img/"+newName);
            return  albumService.updateBanner(album);
        }else {
            return  albumService.updateBanner(album);
        }

    }



}
